/*
 *  Time_ISO8601.cpp
 *  Workout
 *
 *  Created by Acroname on 4/22/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "Time_ISO8601.h"

int SetCalendarDate(int year, int month, int day){
  int resultYear = year;
  int resultMonth = month;
  int resultDay = day;
  
  int result = 0;
  
  char incrementMonthFlag = 0;
  char incrementYearFlag = 0;
  
  do {
    
    // Chip away at the days based on the month 
    // Look at months with 30 days in them
    if (resultMonth == 2) {
      if (resultDay > 28) {
	incrementMonthFlag = 1;
	// Increment the month counter
	resultMonth++;
	
	// Subtract the days
	resultDay -= 28;
      } else {
	incrementMonthFlag = 0;			
      }
    }
    if (resultMonth == 4 || 
	resultMonth == 6 ||
	resultMonth == 9 ||
	resultMonth == 11) {
      // Days must be less than or equal to 30
      // We are incrementing too many days
      if (resultDay > 30) {
	incrementMonthFlag = 1;
	resultMonth++;
	resultDay -= 30;
      }
      else {
	incrementMonthFlag = 0;
      }
    } // End if for 30 days check
    
    // Look at months with 31 days in them
    if (resultMonth == 1 ||
	resultMonth == 3 ||
	resultMonth == 5 ||
	resultMonth == 7 ||
	resultMonth == 8 ||
	resultMonth == 10 ||
	resultMonth == 12 ) {
      
      // We are incrementing too many days
      if (resultDay > 31) {
	if (resultMonth == 12) {
	  resultMonth = 1;
	  resultYear++;
	  resultDay -= 31;
	} else {
	  incrementMonthFlag = 1;
	  resultMonth++;
	  resultDay -= 31;
	}
      } 
      else {
	incrementMonthFlag = 0;
      }
      
    } // end if for 31 day months
    
  } while (incrementYearFlag || incrementMonthFlag); // End of whittling down the days
  
  
  result = (resultYear*10000) + (resultMonth*100) + resultDay;
  
  return result;
}

int SetCalendarDateInterval(int isodate, int numberdays){
  
  int currentDay = 0;
  int currentMonth = 0;
  int currentYear = 0;
  
  int result = 0;
  
  // Grab the current day of the year/month
  currentDay = isodate % 100;
  
  // Grab the current month
  currentMonth = ((isodate - currentDay) % 10000) / 100;
  
  // Grab the current year
  currentYear = (isodate - currentDay - currentMonth) / 10000;
  
  result = SetCalendarDate(currentYear,currentMonth,currentDay + numberdays);
  
  return result;
}


////////////////////////////////////////////////////////////////////////////////


const char * GetCalendarDateMonth(int isodate)
{
  
  // Grab the pointer to the buffer
  const char * pB;
  
#if 0
  
  printf("GetCalendarDateMonth isodate = %d\n",isodate);
#endif
  
  int year = isodate/10000;
  int month = (isodate - year*10000) / 100;
  //int day = isodate - year*10000 - month*100;
  
  switch (month) {
    case 1:
      return "January";
    case 2:
      return "February";
    case 3:
      return "March";
    case 4:
      return "April";
    case 5:
      return "May";
    case 6:
      return "June";
    case 7:
      return "July";
    case 8:
      return "August";
    case 9:
      return "September";
    case 10:
      return "October";
    case 11:
      return "November";
    case 12:
      return "December";
    default:
      break;
  }
  
  return pB;
  
}

////////////////////////////////////////////////////////////////////////////////

int GetCalendarDateDay(int isodate)
{
  
  int year = isodate/10000;
  int month = (isodate - year*10000) / 100;
  int day = isodate - year*10000 - month*100;
  
  return day;
  
}

////////////////////////////////////////////////////////////////////////////////

int GetCalendarDateYear(int isodate)
{
  
  int year = isodate/10000;
  
  return year;
  
}




